﻿using System;
using System.Security.Cryptography;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;


namespace GUI
{
    public partial class FrmLogin : DevComponents.DotNetBar.Office2007Form
    {
        public FrmLogin()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnDangNhap_Click(object sender, EventArgs e)
        {
            MessageBoxEx.EnableGlass = false;
            var flag = false;

            var username = txtUsername.Text.ToString();
            var password = txtPassword.Text.ToString();
            string enPass = GetMD5(password);
            var login = new BLL.Login();
            flag = login.DoLogin(username, enPass);
            if (flag)
            {
                var member = Session.Member;
                if (member.MaVaiTro.Equals(1))
                {
                    foreach (Form f in Application.OpenForms)
                    {
                        if (f is FrmMain)
                        {
                            ((FrmMain) f).EnableMenuQuanLy();
                            ((FrmMain) f).EnablebtnDangXuat();
                            ((FrmMain) f).EnableAllMenu();
                            ((FrmMain) f).EnablebtnDiemDanh();
                        }
                    }
                }
                else if(member.MaVaiTro.Equals(2) || member.MaVaiTro.Equals(4))
                {
                    foreach (Form f in Application.OpenForms)
                    {
                        if (f is FrmMain)
                        {
                            ((FrmMain) f).EnablebtnDangXuat();
                            ((FrmMain) f).EnableAllMenu();
                        }
                    }
                }
                else if(member.MaVaiTro.Equals(3))
                {
                    foreach (Form f in Application.OpenForms)
                    {
                        if (f is FrmMain)
                        {
                            ((FrmMain) f).EnablebtnDangXuat();
                            ((FrmMain) f).EnablebtnDiemDanh();
                            ((FrmMain) f).EnableAllMenu();
                        }
                    }
                }
                else
                {
                    MessageBoxEx.Show(@"Bạn phải chờ admin cấp quyền mới có thể đăng nhập !!!");
                }
                Dispose();
            }
            else
            {
                MessageBoxEx.Show(@"Tài khoản không tồn tại.", @"Warning");
                MessageBoxEx.EnableGlass = false;
                txtUsername.Text = "";
                txtPassword.Text = "";
                txtUsername.Focus();
            }
        }

        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            AcceptButton = btnDangNhap;
        }

        private void btnNhapLai_Click(object sender, EventArgs e)
        {
            txtUsername.Text = "";
            txtPassword.Text = "";
            txtUsername.Focus();
        }

        private void lblQuenPass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Dispose();
            frmQuenPass pass = new frmQuenPass();
            pass.Show();
        }

        public string GetMD5(string password)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(password);

            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);

            foreach (byte b in mang)
            {
                str_md5 += b.ToString();
            }

            return str_md5;
        }
    }
}